#include <stdlib.h>
#include "sikle.h"

typedef struct DictNode {
  Word word;
  struct DictNode* next;
} DictNode;

struct Dictionary {
  DictNode* list[HASH_BUCKET_SIZE];
};

Dictionary* Dictionary_new()
{
  int i;
  Dictionary* dict = (Dictionary*)malloc(sizeof(Dictionary));

  for(i = 0; i < 11; i++) {
    dict->list[i] = NULL;
  }

  return dict;
}

void Dictionary_delete(Dictionary* dict)
{
  return;
}

void Dictionary_put_word(Dictionary* dict, Word word)
{
  int hash = NString_hash(word.name) % HASH_BUCKET_SIZE;

  DictNode* node = NULL;

  for(node = dict->list[hash]; node != NULL; node = node->next) {
    if(NString_equal(node->word.name, word.name)) { /* got a record */
      node->word = word;
      return;
    }
  }

  /* no record */
  node = (DictNode*)malloc(sizeof(DictNode));
  node->word = word;

  node->next = dict->list[hash];
  dict->list[hash] = node;
}

Word* Dictionary_get_word(Dictionary* dict, NString name)
{
  int hash = NString_hash(name) % HASH_BUCKET_SIZE;
  DictNode* node = NULL;

  for(node = dict->list[hash]; node != NULL; node = node->next) {
    if(NString_equal(node->word.name, name)) { /* got a record */
      return &(node->word);
    }
  }

  return NULL;
}
